package service

import (
	"errors"
	"github.com/go-programming-tour-book/blog-service/internal/model"
)

type LoginRequest struct {
	Email    string `form:"email" binding:"required"`
	Password string `form:"password" binding:"required"`
}

type RegisterRequest struct {
	Name     string `form:"name" binding:"required"`
	Email    string `form:"email" binding:"required"`
	Password string `form:"password" binding:"required"`
}

func (svc *Service) Login(param *LoginRequest) (model.User, error) {
	var (
		user model.User
		err  error
	)
	user, err = svc.dao.Login(
		param.Email,
		param.Password,
	)
	if err != nil {
		return user, err
	}

	if user.ID > 0 {
		return user, nil
	}

	return user, errors.New("user info does not exist")
}

func (svc *Service) Register(param *RegisterRequest) (model.User, error) {
	var (
		user model.User
		err  error
	)
	user, err = svc.dao.Register(
		param.Name,
		param.Email,
		param.Password,
	)
	if err != nil {
		return user, err
	}

	if user.ID > 0 {
		return user, nil
	}

	return user, errors.New("user register error")
}
